<?php 
/**
 * 注册
 */
class c_register extends core_base_c {
	/**
	 * 注册
	 * @param string $inPath
	 */
	function pageindex($inPath) {
		$ajax = (int)$_GET['ajaxpost'];
		if($_POST){
			$msg['state']=0;
			$err = '';
			$backUrl = $_POST['url'];
			$post = SUtil::specialhtml($_POST);
			$username = $post['username'];
			$email = $post['email'];
			$password = $post['pwd'];
			if(SUtil::IsUsername($username)===false){
				$err="用户名不合法，需要4-16个字符且首位不能够为数字。";
			}
			if(SUtil::IsMail($email)===false){
				$err="邮箱格式错误";
			}
			if(strlen($password)<6){
				$err="密码长度不能够小于6位";
			}
			if($err==''){
				$mUser = new core_m_user();
				$rs = $mUser->get("username='{$username}'");
				if($rs){
					$err="用户名已经存在。";
				}
				$rs = $mUser->get("email='{$email}'");
				if($rs){
					$err="邮箱已经存在。";
				}
			}
			if($err){
				if($ajax==1){
					$msg['msg'] = $err;
					return json_encode($msg);
				}else{
					$this->ShowMsg($err);
				}
			}
			$uid = $mUser->reg(array('username'=>$username,'email'=>$email,'password'=>md5($password)));
			if($uid){
				$msg['state']=1;
				$err='注册成功！';
			}else{
				$err='注册失败！';
			}
			if($ajax==1){
				$msg['msg'] = $err;
				$msg['url'] = $backUrl;
				return json_encode($msg);
			}else{
				$this->redirect('/login/index');
			}
		}
		$this->params ['url'] = $_GET['url'];
		if($ajax==1){
			return $this->render("register/ajax.html",$this->params);
		}else{
			return $this->render("register/index.html",$this->params);
		}
	}
	/**
	 * 找回密码
	 * @param string $inPath
	 */
	function pagefindpwd($inPath) {
		if($_POST){
			$post = SUtil::specialhtml($_POST);
			$email = $post['email'];
			$username = $post['username'];
			if(SUtil::IsUsername($username)===false){
				$err="用户名不合法。";
			}
			if(SUtil::IsMail($email)===false){
				$err="邮箱格式错误。";
			}
			if(!$err){
				$mUser = new core_m_user();
				$rs = $mUser->get("username='{$username}' and email='{$email}'");
				if($rs){
					$mUser->update("username='{$username}'",array('logintime'=>time(),'islock'=>1));
					$key = md5($rs['username'].$rs['email'].core_base_Constant::COOKIE_KEY);
					$url = core_base_Constant::WEB_URL.$this->createUrl('/register/changepwd')."?username={$username}&key={$key}";
					$mail = new SMail();
					$mail->Send();
					$this->params['user'] = $rs;
					return $this->render("register/findpwd.html",$this->params);
				}else{
					$err="用户名和邮箱不匹配。";
				}
			}
			$this->ShowMsg($err);
		}
		return $this->render("register/findpwd.html",$this->params);
	}
	/**
	 * 找回密码成功页面
	 */
	public function pagechangepwd($inPath){
		$key = $_GET['key'];
		$username = $_GET['username'];
		$mUser = new core_m_user();
		$rs = $mUser->get("username='{$username}'");
		if($rs){
			if(time()-$rs['logintime']<=3600){
				if($key == md5($rs['username'].$rs['email'].core_base_Constant::COOKIE_KEY)){
					$newpwd = SUtil::random(8);
					$mUser->update("username='{$username}'",array('logintime'=>time(),'islock'=>0,'password'=>md5($newpwd)));
					$rs ['newpwd'] = $newpwd;
					$this->params ['user'] = $rs;
					return $this->render("register/changepwd.html",$this->params);
				}else{
					$err = "无效的链接。";
				}
			}else{
				$err = "链接过期，请重新找回。";
			}
		}
		$this->ShowMsg("找回密码失败！".$err);
	}
}
?>